package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.AdmUser;
import com.bishe.psy.model.ConUser;
import com.bishe.psy.model.LikeArticle;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.ILikeArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-16
 * Time: 17:33
 */

@Slf4j
@Api(tags = "文章点赞接口")
@RestController
@RequestMapping("/article")
public class LikeArticleController {

    @Resource
    private ILikeArticleService iLikeArticleService;

    @ApiOperation("文章点赞操作接口")
    @GetMapping("/isLike")
    public AppResult isLike(HttpServletRequest request,
                            @ApiParam(value = "文章id") @RequestParam("articleId") Long articleId,
                            @ApiParam(value = "用户身份") @RequestParam("currentIdentity")Long currentIdentity) {

        System.out.println("11111111111111111");

        //1.参数校验
        if(articleId == null || articleId <= 0 || currentIdentity == null || currentIdentity <= 0 ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        HttpSession session = request.getSession(false);
        Long userId ;
        //创造对象
        LikeArticle likeArticle1 = new LikeArticle();

        likeArticle1.setArticleId(articleId);
        likeArticle1.setCurrentIdentity(currentIdentity);
        if(currentIdentity == 1) {
            StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);
            likeArticle1.setUserId(stuUser.getId());
            userId = stuUser.getId();
            System.out.println(stuUser.getId());

        } else if(currentIdentity == 2) {
            ConUser conUser  = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            likeArticle1.setUserId(conUser.getId());
            userId = conUser.getId();
            System.out.println(conUser.getId());

        } else {
            AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);
            likeArticle1.setUserId(admUser.getId());
            userId = admUser.getId();
            System.out.println(admUser.getId());
        }

        //2.调用service，查询点赞表是否有该用户的点赞记录
        LikeArticle likeArticle = iLikeArticleService.selectLikeById(articleId, userId, currentIdentity);

        System.out.println("222222222");

        //3.参数校验
        if(likeArticle == null) {
            //说明当前用户从来就没有点赞过该片文章
            iLikeArticleService.addOneLikeLogs(likeArticle1);
            return AppResult.success_like();
        } else {
            if (likeArticle.getIsLike() == 1) {
                //说明用户已经点赞过该篇文章,修改点赞状态和文章数量
                iLikeArticleService.updateIsLikeState(likeArticle.getId());
                return AppResult.success_no_like();

            } else {
                iLikeArticleService.updateIsLikeState(likeArticle.getId());
                return AppResult.success_like();
            }
        }
    }
}
